class System::MagazinesController < ApplicationController
  
  before_filter :authorize, :except => [:index, :list_magazines, :show]
  
# Actions relacionadas a revistas
  def list_magazines
    @magazine_pages, @magazines = paginate :magazines, :per_page => 5
  end
  
  def show
    @magazine = Magazine.find(params[:id])
  end
  
  def index
    redirect_to :action => :list_magazines
  end
  
  def self.validateuniq(magazine_id, user_id)
    cl = CirculatonList.find(:all, :conditions => [ "magazine_id = :magazine_id and user_id = :user_id",
                                                    {:magazine_id => magazine_id,:user_id => user_id}] )
    if (cl != nil) 
      return false
    else 
      return true
    end
  end
  
  
  def sign_magazine
    magazine_id = params[:magazine_id]
    user_id = session[:user_id]

    res = CirculationList.count :conditions => [ "magazine_id = ? AND user_id = ?", magazine_id, user_id]


    if (res == 0)
      circulation_list = CirculationList.new()
      circulation_list.user_id = user_id
      circulation_list.magazine_id = magazine_id
      if circulation_list.save
        flash[:notice] = 'Lista assinada com sucesso!<br/><br/>'
        redirect_to :action => :list_magazines
      end
    else
      flash[:notice] = 'Revista ja assinada!<br/><br/>'
      redirect_to :action => :list_magazines
    end

  end
# Fim de actions relacionadas a revistas


# Actions relacionadas a listas
  def my_signatures
    user = User.find(session[:user_id])
    @signatures = user.my_signatures()
#    @my_circulation_lists= CirculartionList.find(:all, :conditions => "user_id = #{session[:user_id]}")
  end
  
  def cancel_signature
    cl = CirculationList.find(params[:cl_id])
    cl.destroy
    redirect_to :action => :my_signatures
  end
# Fim de actions relacionadas a listas


# Actions relacionadas a notificacoes
  def receive_magazine
    priority_list = PriorityList.find(params[:id])
    priority_list.has_magazine = true
    priority_list.retrieve_date = Time.now
    if priority_list.save
      flash[:notice] = "Revista recebida com sucesso"
      redirect_to :action => :my_signatures
    end
  end
  
  def deliver_magazine
    priority_list = PriorityList.find(params[:id])
    priority_list.has_magazine = false
    
    
    if priority_list.save
      #Gambi... 3 dias
      flash[:notice] = ""
      if (Time.now > priority_list.retrieve_date + (24))
        #multa neellee...
        cl = CirculationList.find(:first, :conditions => [ "magazine_id = ? AND user_id = ?", priority_list.edition.magazine_id, priority_list.user_id])
        cl.fine +=1
        cl.save
        flash[:notice] = "Devolucao com atraso. Voce levou multa<br/>"
      end
      
      next_user = priority_list.calculate_next_reader(priority_list.edition_id, priority_list.id)
      if (next_user == nil)
        flash[:notice] += "Revista entregue com sucesso. Nao ha proximo funcionario na lista."
      else
        flash[:notice] += "Revista entregue com sucesso. Mandar revista para " + next_user
      end
      redirect_to :action => :my_signatures
    end    
  end
# Fim de actions relacionadas a notificacoes


# Actions relacionadas a alarmes
  def my_alarms
    user = User.find(session[:user_id])
    @alarms = user.alarms
  end
  
  def mark_alarm_as_read
    alarm_id = params[:id]
    alarm = Alarm.find(alarm_id)
    if alarm.destroy
      flash[:notice] = "Alarme marcado como lido"
      redirect_to :action => :my_alarms
    end
  end
# Fim de actions relacionadas a alarmes
end
